package com.cloudcc.cloudframe.net;

import com.cloudcc.cloudframe.plugin.MessageManager;

import android.text.TextUtils;

/**
 * 封装请求的错误
 * 
 * @author wangzhiqiang
 * @date 2015-7-24 上午9:58:00
 * 
 */
public class ErrorInfo implements ServerStatusCode {

	private int errorCode;
	private String errorMessage;

	public ErrorInfo(int errorCode) {
		this(errorCode, "");
	}

	/**
	 * 获得error
	 * 
	 * @param errorCode
	 * @return
	 */
	public static ErrorInfo get(int errorCode) {
		return new ErrorInfo(errorCode);
	}

	public static ErrorInfo get(int errorCode, String errorMessage) {
		return new ErrorInfo(errorCode, errorMessage);
	}

	public ErrorInfo(int errorCode, String errorMessage) {
		super();
		this.errorCode = errorCode;
		this.errorMessage = errorMessage;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public String getErrorMessage() {

		if (errorMessage == null || TextUtils.isEmpty(errorMessage)) {
			getMessageByCode(errorCode);
		}
		return errorMessage;
	}

	private void getMessageByCode(int code) {
		MessageManager messageManager = MessageManager.getInstance();
		switch (code) {
		case JSON_PARSER_ERROR:
			errorMessage = messageManager.getJsonError();
			break;
		case NET_DATA_EXCEPTION:
			errorMessage = messageManager.getDataNetException();
			break;
		case NET_NO_NETWORK:
			errorMessage = messageManager.getNoNet();
			break;

		default:
			break;
		}
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	@Override
	public String toString() {
		return "RequestError [errorCode=" + errorCode + ", errorMessage="
				+ errorMessage + "]";
	}

}
